package com.hmo.automatic;

import android.accessibilityservice.AccessibilityService;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.RequiresApi;

import java.util.List;

public class CallAccessibilityService extends AccessibilityService {

    private static final String TAG = "hmo无障碍";

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        final int eventType = event.getEventType();
        switch(eventType) {
            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:    // 窗口状态改变
                Log.e(TAG, "窗口状态"+event.toString() );
                break;
            case AccessibilityEvent.TYPE_VIEW_SCROLLED:           //页面变动事件
            case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:  //窗口内容改变
                d(event);
                break;
        }
    }

    private void d(AccessibilityEvent event){
        Log.e(TAG, event.getPackageName().toString() );
        if (event.getPackageName().equals("com.android.contacts")){
            Log.e(TAG, event.toString() );
            performGlobalAction(10);
//            event.getSource().performAction(KeyEvent.KEYCODE_HEADSETHOOK);
//            event.getSource().performAction(AccessibilityNodeInfo.ACTION_NEXT_HTML_ELEMENT);
        }

    }

    private void demo(AccessibilityEvent event){
        if (event.getPackageName()!="com.hunk.screen.ui")return;
        AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
        if (rootInActiveWindow == null) {
            return;
        }
        Log.e(TAG, rootInActiveWindow.toString() );
        List<AccessibilityNodeInfo> accessibilityNodeInfosByViewId = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.example.benyue:id/answer");
        for (AccessibilityNodeInfo info :accessibilityNodeInfosByViewId){
            info.performAction(AccessibilityNodeInfo.ACTION_CLICK);
        }
    }

    private void onCall(AccessibilityEvent event){
        //Log.e(TAG, event.toString());
        if (!event.getPackageName().equals("com.android.incallui"))return;
        AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
        if (rootInActiveWindow == null) {
            return;
        }
        Log.e(TAG, rootInActiveWindow.toString() );
        performGlobalAction(10);
        //oneByOne(rootInActiveWindow);
//        List<AccessibilityNodeInfo> infos = rootInActiveWindow.findAccessibilityNodeInfosByText("接听");
//        if (infos != null && !infos.isEmpty()) {
//            for (AccessibilityNodeInfo nodeInfo : infos) {
//                if (nodeInfo != null) {
//                    Log.e(TAG, "寻找到了"+nodeInfo.toString() );
//                    nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                    nodeInfo.setChecked(true);
//                    nodeInfo.setSelected(true);
//                    nodeInfo.setFocused(true);
//                    return;
//                }
//            }
//        }
//        List<AccessibilityNodeInfo> nodeInfoList = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.android.incallui:id/answerbutton");
//        if (nodeInfoList != null && !nodeInfoList.isEmpty()) {
//            for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
//                if (nodeInfo != null) {
//                    nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                    return;
//                }
//            }
//        }
    }

    private void oneByOne(AccessibilityNodeInfo rootInActiveWindow){
        int childCount = rootInActiveWindow.getChildCount();
        if (childCount==0)return;
        for (int i=0;i<childCount;i++){
            AccessibilityNodeInfo child = rootInActiveWindow.getChild(i);
            Log.e(TAG, "One: "+i+"|"+child.toString() );
            if (child.getClassName().equals("com.android.incallui.widget.multiwaveview.TargetDrawable")){
                if (child.getText().equals("接听")){
                    child.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    child.performAction(AccessibilityNodeInfo.ACTION_SELECT);
                    child.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
                    child.performAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
                }
            }
            oneByOne(child);
        }
    }

    @Override
    public void onInterrupt() {
        Log.e(TAG, "onInterrupt: ");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG, "onUnbind: "+intent.toString() );
        return super.onUnbind(intent);
    }
}
